//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using AjaxControlToolkit;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Controls;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ProductPicturesControl : BaseNopAdministrationUserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);          

            dlImages.ItemDataBound += new DataListItemEventHandler(dlImages_ItemDataBound);
            dlImages.ItemCommand += new DataListCommandEventHandler(dlImages_ItemCommand);
            btnUploadProductPicture.Click += btnUploadProductPicture_Click;

            //ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(btnUploadProductPicture);
            ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnUploadProductPicture);

            //fuProductPicture.UploadComplete += new EventHandler<AjaxFileUploadEventArgs>(fuProductPicture_UploadComplete);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!Page.IsPostBack) {
                this.BindData();
            }
        }

        public void SaveInfo()
        {
        }

        private void BindData()
        {
            var product = this.ProductService.GetProductById(this.ProductId);
            if (product != null) {
                pnlData.Visible = true;
                pnlMessage.Visible = false;

                var productPictures = product.ProductPictures;
                if (productPictures.Count > 0) {
                    dlImages.Visible = true;
                    dlImages.DataSource = productPictures;
                    dlImages.DataBind();
                } else {
                    dlImages.Visible = false;
                }
            } else {
                pnlData.Visible = false;
                pnlMessage.Visible = true;
            }
        }

        /*
        void fuProductPicture_UploadComplete(object sender, AjaxFileUploadEventArgs e) {
            var product = this.ProductService.GetProductById(this.ProductId);
            if (product != null) {

                // set index or default is 1
                int pictureIndex = 1;
                if (product.ProductPictures != null) {
                    if (product.ProductPictures.Count <= 0) {
                        pictureIndex = product.ProductPictures.Count;
                    }
                }

                Picture picture = this.PictureService.InsertPicture(e.GetContents(), e.ContentType, true);
                if (picture != null) {
                    var productPicture = new ProductPicture() {
                        ProductId = product.ProductId,
                        PictureId = picture.PictureId,
                        DisplayOrder = pictureIndex
                    };
                    this.ProductService.InsertProductPicture(productPicture);
                }

                BindData();
            }
        }
        */

        protected void btnUploadProductPicture_Click(object sender, EventArgs e)
        {
            try {
                var product = this.ProductService.GetProductById(this.ProductId);
                if (product != null) {
                    //fuProductPicture.
                    if (fuProductPicture.HasFile) {
                        Picture picture = this.PictureService.InsertPicture(fuProductPicture.FileBytes, fuProductPicture.PostedFile.ContentType, true);
                        if (picture != null) {
                            var productPicture = new ProductPicture() {
                                ProductId = product.ProductId,
                                PictureId = picture.PictureId,
                                DisplayOrder = txtProductPictureDisplayOrder.Value
                            };
                            this.ProductService.InsertProductPicture(productPicture);
                        }
                    }
                    //if (fuProductPicture2.HasFile) {
                    //    Picture picture = this.PictureService.InsertPicture(fuProductPicture2.FileBytes, fuProductPicture2.PostedFile.ContentType, true);
                    //    if (picture != null) {
                    //        var productPicture = new ProductPicture() {
                    //            ProductId = product.ProductId,
                    //            PictureId = picture.PictureId,
                    //            DisplayOrder = txtProductPictureDisplayOrder2.Value
                    //        };
                    //        this.ProductService.InsertProductPicture(productPicture);
                    //    }
                    //}
                    //if (fuProductPicture3.HasFile) {
                    //    Picture picture = this.PictureService.InsertPicture(fuProductPicture3.FileBytes, fuProductPicture3.PostedFile.ContentType, true);
                    //    if (picture != null) {
                    //        var productPicture = new ProductPicture() {
                    //            ProductId = product.ProductId,
                    //            PictureId = picture.PictureId,
                    //            DisplayOrder = txtProductPictureDisplayOrder3.Value
                    //        };
                    //        this.ProductService.InsertProductPicture(productPicture);
                    //    }
                    //}
                    BindData();
                }
            } catch (Exception exc) {
                ProcessException(exc);
            }
        }

        void dlImages_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                ProductPicture productPicture = (ProductPicture)e.Item.DataItem;
                if (productPicture != null) {
                    Image iProductPicture = e.Item.FindControl("iProductPicture") as Image;
                    if (iProductPicture != null)
                        iProductPicture.ImageUrl = this.PictureService.GetPictureUrl(productPicture.PictureId);
                }
            }
        }

        void dlImages_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandArgument == null)
                return;

            int productPictureId = int.Parse(e.CommandArgument.ToString());
            ProductPicture productPicture = this.ProductService.GetProductPictureById(productPictureId);

            switch (e.CommandName) {
                case "UpdateProductPicture":
                    NumericTextBox txtProductPictureDisplayOrder = e.Item.FindControl("txtProductPictureDisplayOrder") as NumericTextBox;
                    productPicture.DisplayOrder = txtProductPictureDisplayOrder.Value;
                    ProductService.UpdateProductPicture(productPicture);
                    break;
                case "DeleteProductPicture":
                    ProductService.DeleteProductPicture(productPicture.ProductPictureId);
                    PictureService.DeletePicture(productPicture.PictureId);
                    break;
            }

            BindData();
        }

        //protected void gvwImages_RowCommand(object sender, GridViewCommandEventArgs e) {
        //    if (e.CommandName == "UpdateProductPicture") {
        //        int index = Convert.ToInt32(e.CommandArgument);
        //        GridViewRow row = gvwImages.Rows[index];
        //        NumericTextBox txtProductPictureDisplayOrder = row.FindControl("txtProductPictureDisplayOrder") as NumericTextBox;
        //        HiddenField hfProductPictureId = row.FindControl("hfProductPictureId") as HiddenField;

        //        int displayOrder = txtProductPictureDisplayOrder.Value;
        //        int productPictureId = int.Parse(hfProductPictureId.Value);
        //        ProductPicture productPicture = this.ProductService.GetProductPictureById(productPictureId);

        //        if (productPicture != null) {
        //            productPicture.DisplayOrder = displayOrder;
        //            this.ProductService.UpdateProductPicture(productPicture);
        //        }

        //        BindData();
        //    }
        //}

        //protected void gvwImages_RowDataBound(object sender, GridViewRowEventArgs e) {
        //    if (e.Row.RowType == DataControlRowType.DataRow) {
        //        ProductPicture productPicture = (ProductPicture)e.Row.DataItem;
        //        Image iProductPicture = e.Row.FindControl("iProductPicture") as Image;
        //        if (iProductPicture != null)
        //            iProductPicture.ImageUrl = this.PictureService.GetPictureUrl(productPicture.PictureId);

        //        Button btnUpdate = e.Row.FindControl("btnUpdate") as Button;
        //        if (btnUpdate != null)
        //            btnUpdate.CommandArgument = e.Row.RowIndex.ToString();
        //    }
        //}

        //protected void gvwImages_RowDeleting(object sender, GridViewDeleteEventArgs e) {
        //    int productPictureId = (int)gvwImages.DataKeys[e.RowIndex]["ProductPictureId"];
        //    ProductPicture productPicture = this.ProductService.GetProductPictureById(productPictureId);
        //    if (productPicture != null) {
        //        this.ProductService.DeleteProductPicture(productPicture.ProductPictureId);
        //        this.PictureService.DeletePicture(productPicture.PictureId);
        //        BindData();
        //    }
        //}

        public int ProductId
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductId");
            }
        }
    }
}